/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang xiangyun<1327295677@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  sqlite.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(29/04/22)
 *         Author:  Zhang xiangyun <1327295677@qq.com>
 *      ChangeLog:  1, Release initial version on "29/04/22 11:06:56"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "logger.h"

int sqlite_init(sqlite3 **db);
static int callback(void *NotUsed,int argc,char **argv,char **azColName);
int sqlite_create_table(char *table_name,sqlite3 *db);
int sqlite_data_insert(char *databuf[],char *table_name,sqlite3 *db);
int determine_table_exist(char *table_name,sqlite3 *db);
static int exist_callback(void *data,int argc,char **argv,char **azColName);

/* open database "server.db" */
int sqlite_init(sqlite3 **db)
{
	int rc =-1;
	rc=sqlite3_open("server.db",db);
	if(rc!=0)
	{
		log_error("open database failure:%s\n",sqlite3_errmsg(*db));
		return -2;
	}
	else
	{
		log_info("open database success!\n");
	}

}

static int callback(void *NotUsed,int argc,char **argv,char **azColName)
{
	int i;
	for(i=0;i<argc;i++)
	{
		log_info("%s=%s\n",azColName[i],argv[i]?argv[i]:"NULL");
	}
	log_info("\n");
	return 0;
}

/* create table in database */
int sqlite_create_table(char *table_name,sqlite3 *db)
{
	int    rc=-1;
	char  *zErrMsg=0;
	char   sql[100];

	memset(sql,0,sizeof(sql));
	sprintf(sql,"CREATE TABLE %s(NAME CHAR(10),DATATIME CHAR(50),TEMPERATURE CHAR(10));",table_name);
	rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);
	if(rc != SQLITE_OK)
	{
		log_error("SQL error:%s\n",zErrMsg);
		sqlite3_free(zErrMsg);
		return -2;
	}
	else
	{
		log_info("Table create successfully!\n");
	}
	return 0;
}


/* insert data to table */
int sqlite_data_insert(char *databuf[],char *table_name,sqlite3 *db)
{
	char     sql[512];
	int      rc=-1;
	char    *zErrMsg=0;
	char    *NAME=databuf[0];
	char    *TIME=databuf[1];
	char    *TEMPERATURE=databuf[2];

	printf("NAME:%s\n",databuf[0]);
	printf("TIME:%s\n",databuf[1]);
	printf("TEMPERATURE:%s\n",databuf[2]);

	memset(sql,0,sizeof(sql));
	sprintf(sql,"INSERT INTO %s VALUES('%s','%s','%s');",table_name,databuf[0],databuf[1],databuf[2]);
	rc=sqlite3_exec(db,sql,callback,0,&zErrMsg);

	if(rc!=SQLITE_OK)
	{
		log_error("sqlite insert error:%s\n",zErrMsg);
		sqlite3_free(zErrMsg);
		return -2;
	}
	else
	{
		log_info("data insert into database success!\n");
	}
	return 0;
}

/* determine whether the table exists */
int determine_table_exist(char *table_name,sqlite3 *db)
{
	char  sql[512];
	int   exist=-1;
	int   rc=-1;
	char *zErrMsg=0;

	sprintf(sql,"SELECT COUNT(*) FROM sqlite_master where type ='table' and name ='%s';",table_name);
	rc=sqlite3_exec(db,sql,exist_callback,&exist,&zErrMsg);

	if(rc!=SQLITE_OK)
	{
		log_error("determine_table_exist()failure:%s\n",zErrMsg);
		sqlite3_free(zErrMsg);
		exit(0);
	}
	if(exist)
	{
		log_info("%s is already exist\n",table_name);
		return 0;
	}
	else
	{
		return 1;
	}
}

static int exist_callback(void *data,int argc,char **argv,char **azColName)
{
	if(1==argc)
	{
		*(int *)data = atoi(*(argv));
	}
	return 0;
}

